package pt.isec.gpsgroup5.moneybox;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class CheckInManual extends Activity {

	private DBAdapter db;
	private ListView lv;
	AlertDialog.Builder alert;
	AlertDialog.Builder result;
	Context context;
	EditText input;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_objective);

		context = this;

		db = new DBAdapter(this);
		db.open(this);

		lv = (ListView) findViewById(R.id.list);

		SimpleAdapter sa = new SimpleAdapter(this, loadList(),
				R.layout.list_obj_item, new String[] { DbHelper.NAME,
						"percent", DbHelper.CURRENT_AMOUNT,
						DbHelper.FINAL_AMOUNT, "deadline", "priority" },
				new int[] { R.id.objName, R.id.objPercent,
						R.id.objCurrentAmount, R.id.objFinalAmount,
						R.id.objDeadline, R.id.objPriority });

		lv.setAdapter(sa);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {

				alert = new AlertDialog.Builder(context);
				alert.setTitle("Amount");
				input = new EditText(context);
				alert.setView(input);
				
				alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						AlertDialog.Builder bdialog = new AlertDialog.Builder(context);
						bdialog.setTitle("");
						bdialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								finish();
							}
						});
						
						Double value = (double) 0;
						
						try{
							value = Double.parseDouble(input.getText().toString());
						}catch(Exception e){
							bdialog.setMessage("Not a valid value");
							AlertDialog rdialog = bdialog.create();
							rdialog.show();
						}
						
						Objective obj = db.getObjectivePos( arg2 );
						
						
						
						if(obj.getCurrentAmount() + value <= obj.getFinalAmount()){
							obj.setCurrentAmount(obj.getCurrentAmount()+value);
							db.updateObjective(obj, obj.getId());
							bdialog.setMessage(R.string.checkinManualComplete);
							AlertDialog rdialog = bdialog.create();
							rdialog.show();
							
						} else {
							bdialog.setMessage(R.string.checkinManualFull);
							AlertDialog rdialog = bdialog.create();
							rdialog.show();
						}
					}
				});
				alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				});
				
				alert.show();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_check_in_manual, menu);
		return true;
	}

	private ArrayList<HashMap<String, String>> loadList() {

		ArrayList<HashMap<String, String>> objMap = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> tmpObj;
		Objective obj = null;
		int percent = -1;

		for (int i = 0; i < db.numObjectives() ; i++) {

			obj = db.getObjectivePos(i);
			tmpObj = new HashMap<String, String>();
			tmpObj.put(DbHelper.NAME, obj.getName());
			percent = (int) ((100 * obj.getCurrentAmount()) / obj
					.getFinalAmount());
			tmpObj.put("percent", "" + percent + "%");
			tmpObj.put(DbHelper.CURRENT_AMOUNT, "" + obj.getCurrentAmount());
			tmpObj.put(DbHelper.FINAL_AMOUNT, "" + obj.getFinalAmount());
			String deadline = obj.getDay() + "-" + obj.getMonth() + "-"
					+ obj.getYear();
			tmpObj.put("deadline", "" + deadline);
			tmpObj.put("priority", "" + obj.getPriority());
			objMap.add(tmpObj);
		}

		return objMap;
	}

}
